﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PowerTriggers.ServiceReference;
using System.Management;

namespace PowerTriggers
{
    internal partial class SelectUserName : Form
    {
        private SelectUserName()
        {
            InitializeComponent();
        }

        public SelectUserName(string username, string password)
            : this()
        {
            textBox1.Text = string.IsNullOrEmpty(username) ? string.Empty : Program.DecryptString(username);
            textBox2.Text = string.IsNullOrEmpty(password) ? string.Empty : Program.DecryptString(password);
        }

        internal string Username
        {
            get
            {
                return string.IsNullOrEmpty(textBox1.Text.Trim()) ? string.Empty : Program.EncryptString(textBox1.Text.Trim());
            }
        }

        internal string Password
        {
            get
            {
                return string.IsNullOrEmpty(textBox2.Text) ? string.Empty : Program.EncryptString(textBox2.Text);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                string username = textBox1.Text.Trim();
                string password = textBox2.Text;
                bool isValid = ValidateUserCredentials(ref username, password);
                textBox1.Text = username;
                if (isValid)
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            catch (System.Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                string username = textBox1.Text.Trim();
                string password = textBox2.Text;
                bool isValid = ValidateUserCredentials(ref username, password);
                textBox1.Text = username;
                if (isValid)
                {
                    MessageBox.Show(this, "Username and password successfully authenticated", "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private bool ValidateUserCredentials(ref string username, string password)
        {
            try
            {
                bool isValid;
                string errorMsg;
                if (username.Length == 0)
                {
                    isValid = false;
                    errorMsg = "You must enter a username";
                }
                else
                {
                    using (var client = WcfClientFactory.OpenWcfClient())
                    {
                        string usernameX = this.Username;
                        string passwordX = this.Password;
                        isValid = client.AuthenticateUser(ref usernameX, out errorMsg, passwordX);
                        username = Program.DecryptString(usernameX);
                    }
                }
                if (!isValid && !string.IsNullOrEmpty(errorMsg))
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(this, errorMsg, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (textBox1.CanFocus)
                    {
                        textBox1.Focus();
                    }
                }
                return isValid;
            }
            catch (System.Exception ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(this, "Error Authenticating: " + ex.Message, "Power Triggers", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
    }
}
